package com.sense.kukoo.biz_explore.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.sense.kukoo.biz_explore.adapter.SearchAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.repo.bean.SearchBean
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.repo.http.bean.UserSearchBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class SearchResultUserItemProvider(val listener: SearchAdapter.SearchEventListener):
    MultiItemProvider<SearchBean>() {

    private var mCurrentUserId = 0L

    override fun getItemType(): Int = SearchBean.TYPE_SEARCH_RESULT_USER

    override fun getItemLayoutId(): Int = R.layout.common_search_result_user_item

    override fun onBindViewHolder(viewHolder: BaseViewHolder, bean: SearchBean) {
        val data: UserSearchBean = bean.user!!
        //
        viewHolder.getView(R.id.cl_search_result_user_root).setOnClickListener {
            listener.onSearchResultChannelClick(bean)
        }
        //
        val iv_icon_user = viewHolder.getView(R.id.iv_icon_user) as ImageView
        Monet.get(iv_icon_user.context)
            .load<GlideConfig>(data.avatarUrl)
            .placeholder(com.sense.kukoo.common_design.R.drawable.design_icon_avatar_default)
            .circleCrop()
            .into(iv_icon_user)
        //
        val tv_name_user = viewHolder.getView(R.id.tv_name_user) as TextView
        tv_name_user.text = data.channelName
        //
        val tv_des_user = viewHolder.getView(R.id.tv_des_user) as TextView
        tv_des_user.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.num_followers, data.fansNum)
        //
        val tv_btn_follow = viewHolder.getView(R.id.tv_btn_follow) as TextView
        if(data.id == mCurrentUserId){
            tv_btn_follow.visibility = View.GONE
        } else {
            tv_btn_follow.visibility = View.VISIBLE
            //
            if (data.isFollow()) {
                tv_btn_follow.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.following)
                tv_btn_follow.setTextColor(tv_btn_follow.resources.getColor(com.sense.kukoo.common_design.R.color.text_active))
                tv_btn_follow.setBackgroundResource(com.sense.kukoo.common_design.R.drawable.design_bg_btn_follow_active)
            } else {
                tv_btn_follow.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.follow)
                tv_btn_follow.setTextColor(tv_btn_follow.resources.getColor(com.sense.kukoo.common_design.R.color.text_white))
                tv_btn_follow.setBackgroundResource(com.sense.kukoo.common_design.R.drawable.design_bg_btn_follow_def)
            }
            tv_btn_follow.setOnClickListener {
                listener.onSearchResultFollowClick(bean, viewHolder.adapterPosition)
            }
        }
    }

    fun setCurrentUserId(currentUserId:Long){
        mCurrentUserId = currentUserId
    }
}